﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using mscook.core;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.mvc.core;
using mscook.core.services;
using System.Web.Security;
using mscook.mvc.Core;
using System.Linq.Expressions;

namespace mscook.mvc.Areas.weblogin.Controllers
{
    [UserAuthorizeAttribute(Roles = "admin")]
    public class AccountController : BaseController
    {
        //
        // GET: /weblogin/Account/

        private ILoginUserService iLoginUserService;
        public AccountController(ILoginUserService iLoginUserService)
            : base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
        }

        public ActionResult Index(string nowPageNum, string email = "", string loginName = "", string per = "")
        {
            Expression<Func<LoginUser, bool>> select_expression = o => (string.IsNullOrEmpty(loginName) || o.LoginName.Contains(loginName))
              && (string.IsNullOrEmpty(email) || o.Email.Contains(email));

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<LoginUser> loginUserList = iLoginUserService.SelectLimitLoginUserByType(false, pageHelper.Start, pageHelper.PerValue, out total, select_expression);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitNavBar();
            ViewData["loginName"] = loginName;
            ViewData["email"] = email;
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            return View(loginUserList);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return Redirect("/");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult add()
        {
            InitNavBar();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult add(LoginUser loginUser)
        {
            if (iLoginUserService.SelectLoginUserByLoginNameOrEmail(loginUser.LoginName, loginUser.Email) == null)
            {
                if (ModelState.IsValid)
                {
                    loginUser.IsAnonymous = false;
                    iLoginUserService.SaveOrUpdate(loginUser);
                }
            }

            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string id)
        {
            LoginUser loginUser = iLoginUserService.Get(id);
            if (loginUser != null)
            {
                InitNavBar();
                return View(loginUser);
            }

            return RedirectToIndex();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string Id, string email, LoginUser modelValidate)
        {
            LoginUser loginUser = iLoginUserService.Get(Id);

            if (ModelState.IsValid)
            {
                UpdateModel(loginUser);//2013-12-03 Lampard UpdateModel也会进行数据验证

                iLoginUserService.SaveOrUpdate(loginUser);

            }
            return RedirectToIndex();
        }
        public ActionResult Delete(string id)
        {
            iLoginUserService.Delete(id);
            return RedirectToIndex();
        }



    }
}
